home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / PASCAL / 0195.ZIP / KEYBOARD.PAS < prev    next >
Pascal/Delphi Source File  |  1984-12-20  |  2KB  |  48 lines

  1. {@@@@@@@@@@@ copyright (C) 1984 by Neil J. Rubenking @@@@@@@@@@@@@@@@@@@@@@@@
  2. The purchaser of these procedures and functions may include them in COMPILED
  3. programs freely, but may not sell or give away the source text.
  4.  
  5.    If the function KeyBoard is invoked with the [W]ait parameter, it
  6.    WAITS for a key to be pressed.  If [N]o wait, it returns zero if
  7.    there's nothing in the keyboard buffer, or calls KeyBoard('W') if
  8.    there is something.  The return is an integer--its low byte is the
  9.    ASCII code of the key pressed (zero if a "special" code key) and
  10.    its high byte is the scan code.  If you call for [N]o wait and no
  11.    key was pressed, both high and low bytes will be zero.
  12.  
  13.    Note that the file SCANCODE.DAT contains a screen picture of the
  14.    keyboard scan codes and a chart of the special codes.
  15.  
  16. }
  17. {$I regpack.typ}
  18. {$I keyboard.lib}
  19. var
  20.   TheseCodes          : integer;
  21.   ASCIIcode, ScanCode : byte;
  22.   ThisCharacter       : char;
  23.  
  24. begin
  25.   WriteLn('Press a key to begin.  Press Escape to end.');
  26.   repeat
  27.     TheseCodes := KeyBoard('W');
  28.     ASCIIcode  := (TheseCodes shl 8) shr 8;
  29.     ScanCode   := TheseCodes shr 8;
  30.  
  31.     { NOTE: I have used "shl" and "shr" here to get the high and low bytes.
  32.       These will be familiar to Assembly language programmers. They mean
  33.       "shift left" and "shift right".  "shl <x>" is a left shift of <x> bits.
  34.       The highest bits are "pushed off" the top end, and <x> zeroes are
  35.       tacked on the low end.  Thus, if a 16-bit integer is shown by
  36.       HHHHHHHHLLLLLLLL, (the Hs begin the High 8 bits and the Ls the Low)
  37.       after a "shl 8" it will look like "LLLLLLLL00000000", and after being
  38.       shifted 8 right again, it will be "00000000LLLLLLLL".  If we just
  39.       "shr 8" to start with, we get "00000000HHHHHHHH".  SHR and SHL are
  40.       invaluable any time you need to do individual bit manipulation.}
  41.  
  42.     if ASCIIcode = 0 then
  43.       WriteLn('Character is "special"')
  44.     else WriteLn('Character is ',chr(ASCIIcode));
  45.     WriteLn('Scan Code is ',ScanCode);
  46.     WriteLn;
  47.   until (ASCIIcode = 27) and (ScanCode = 1);
  48. end.